<template>
	<view class="container">
		<!-- 地址信息 -->
		<view class="address-section" @click="chooseAddress">
			<view class="address-box" v-if="address.name">
				<view class="address-info">
					<view class="name-phone">
						<text class="name">{{ address.name }}</text>
						<text class="phone">{{ address.phone }}</text>
					</view>
					<view class="address-detail">
						<text>{{ address.province }} {{ address.city }} {{ address.district }} {{ address.detail }}</text>
					</view>
				</view>
				<text class="iconfont icon-right"></text>
			</view>
			<view class="no-address" v-else>
				<text>请选择收货地址</text>
				<text class="iconfont icon-right"></text>
			</view>
		</view>
		
		<!-- 商品信息 -->
		<view class="product-section">
			<view class="product-item">
				<image :src="product.image" mode="aspectFill"></image>
				<view class="info">
					<view class="name-spec">
						<text class="name">{{ product.name }}</text>
						<text class="spec">{{ product.specs[selectedSpec] }}</text>
					</view>
					<view class="price-num">
						<text class="price">¥{{ product.price }}</text>
						<view class="number">
							<text>x{{ quantity }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 配送方式 -->
		<view class="delivery-section">
			<view class="section-item">
				<text class="title">配送方式</text>
				<view class="value">
					<text>{{ deliveryMethods[selectedDelivery] }}</text>
					<text class="iconfont icon-right"></text>
				</view>
			</view>
		</view>
		
		<!-- 优惠券 -->
		<view class="coupon-section">
			<view class="section-item">
				<text class="title">优惠券</text>
				<view class="value">
					<text>{{ coupon ? coupon : '无可用' }}</text>
					<text class="iconfont icon-right"></text>
				</view>
			</view>
		</view>
		
		<!-- 订单备注 -->
		<view class="remark-section">
			<view class="section-item">
				<text class="title">订单备注</text>
				<view class="value">
					<input type="text" v-model="remark" placeholder="选填，请填写备注信息" placeholder-class="placeholder" />
				</view>
			</view>
		</view>
		
		<!-- 金额明细 -->
		<view class="amount-section">
			<view class="amount-item">
				<text>商品金额</text>
				<text>¥{{ productAmount }}</text>
			</view>
			<view class="amount-item">
				<text>优惠金额</text>
				<text class="discount">-¥{{ discountAmount }}</text>
			</view>
			<view class="amount-item">
				<text>运费</text>
				<text>+¥{{ deliveryFee }}</text>
			</view>
		</view>
		
		<!-- 底部支付栏 -->
		<view class="footer">
			<view class="total">
				<text>合计：</text>
				<text class="amount">¥{{ totalAmount }}</text>
			</view>
			<view class="submit-btn" @click="submitOrder">提交订单</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			productId: 0,
			selectedSpec: 0,
			quantity: 1,
			product: {
				id: 0,
				name: '',
				price: 0,
				specs: [],
				image: ''
			},
			address: {
				name: '',
				phone: '',
				province: '',
				city: '',
				district: '',
				detail: ''
			},
			deliveryMethods: ['普通配送', '上门自提'],
			selectedDelivery: 0,
			coupon: '',
			remark: '',
			productAmount: 0,
			discountAmount: 0,
			deliveryFee: 0
		}
	},
	computed: {
		totalAmount() {
			return (this.productAmount - this.discountAmount + this.deliveryFee).toFixed(2)
		}
	},
	onLoad(options) {
		if (options.productId) {
			this.productId = options.productId
			if (options.spec) {
				this.selectedSpec = parseInt(options.spec)
			}
			this.getProductDetail()
		}
		
		// 获取默认地址
		this.getDefaultAddress()
	},
	methods: {
		getProductDetail() {
			// 模拟获取商品详情
			setTimeout(() => {
				this.product = {
					id: this.productId,
					name: '新鲜草莓',
					price: 38.8,
					specs: ['250g/盒', '500g/盒', '1kg/盒'],
					image: '/static/product/strawberry.jpg'
				}
				
				// 计算商品金额
				this.productAmount = this.product.price * this.quantity
			}, 500)
		},
		getDefaultAddress() {
			// 模拟获取默认地址
			setTimeout(() => {
				this.address = {
					name: '张三',
					phone: '138****1234',
					province: '广东省',
					city: '深圳市',
					district: '南山区',
					detail: '科技园南路18号'
				}
			}, 500)
		},
		chooseAddress() {
			uni.navigateTo({
				url: '/pages/address/list?from=order'
			})
		},
		submitOrder() {
			if (!this.address.name) {
				uni.showToast({
					title: '请选择收货地址',
					icon: 'none'
				})
				return
			}
			
			uni.showLoading({
				title: '正在提交...'
			})
			
			// 模拟提交订单
			setTimeout(() => {
				uni.hideLoading()
				
				uni.showModal({
					title: '提示',
					content: '订单提交成功，是否立即支付？',
					confirmText: '立即支付',
					cancelText: '稍后支付',
					success: (res) => {
						if (res.confirm) {
							uni.navigateTo({
								url: '/pages/order/pay?orderId=123456'
							})
						} else {
							uni.redirectTo({
								url: '/pages/order/list'
							})
						}
					}
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	padding-bottom: 120rpx;
	background-color: #f5f5f5;
}

.address-section {
	background-color: #fff;
	margin-bottom: 20rpx;
	padding: 30rpx;
	
	.address-box {
		display: flex;
		justify-content: space-between;
		align-items: center;
		
		.address-info {
			flex: 1;
			
			.name-phone {
				display: flex;
				align-items: center;
				margin-bottom: 10rpx;
				
				.name {
					font-size: 32rpx;
					color: #333;
					font-weight: bold;
					margin-right: 20rpx;
				}
				
				.phone {
					font-size: 28rpx;
					color: #666;
				}
			}
			
			.address-detail {
				font-size: 28rpx;
				color: #666;
				line-height: 1.4;
			}
		}
		
		.iconfont {
			font-size: 36rpx;
			color: #999;
		}
	}
	
	.no-address {
		display: flex;
		justify-content: space-between;
		align-items: center;
		color: #666;
		font-size: 30rpx;
		
		.iconfont {
			font-size: 36rpx;
			color: #999;
		}
	}
}

.product-section {
	background-color: #fff;
	margin-bottom: 20rpx;
	padding: 30rpx;
	
	.product-item {
		display: flex;
		
		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 8rpx;
		}
		
		.info {
			flex: 1;
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			
			.name-spec {
				.name {
					font-size: 28rpx;
					color: #333;
					margin-bottom: 10rpx;
				}
				
				.spec {
					font-size: 24rpx;
					color: #999;
					background-color: #f7f7f7;
					padding: 4rpx 12rpx;
					border-radius: 6rpx;
				}
			}
			
			.price-num {
				display: flex;
				justify-content: space-between;
				align-items: center;
				
				.price {
					font-size: 32rpx;
					color: #ff5151;
					font-weight: bold;
				}
				
				.number {
					font-size: 28rpx;
					color: #666;
				}
			}
		}
	}
}

.delivery-section,
.coupon-section,
.remark-section {
	background-color: #fff;
	margin-bottom: 20rpx;
	
	.section-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		
		.title {
			font-size: 28rpx;
			color: #333;
		}
		
		.value {
			display: flex;
			align-items: center;
			
			text {
				font-size: 28rpx;
				color: #666;
			}
			
			input {
				text-align: right;
				font-size: 28rpx;
				color: #666;
			}
			
			.placeholder {
				color: #999;
			}
			
			.iconfont {
				font-size: 36rpx;
				color: #999;
				margin-left: 10rpx;
			}
		}
	}
}

.amount-section {
	background-color: #fff;
	margin-bottom: 20rpx;
	padding: 30rpx;
	
	.amount-item {
		display: flex;
		justify-content: space-between;
		margin-bottom: 20rpx;
		
		text {
			font-size: 28rpx;
			color: #666;
		}
		
		.discount {
			color: #ff5151;
		}
		
		&:last-child {
			margin-bottom: 0;
		}
	}
}

.footer {
	position: fixed;
	left: 0;
	right: 0;
	bottom: 0;
	height: 100rpx;
	background-color: #fff;
	display: flex;
	align-items: center;
	padding: 0 30rpx;
	box-shadow: 0 -1px 5px rgba(0, 0, 0, 0.05);
	
	.total {
		flex: 1;
		display: flex;
		align-items: center;
		
		text {
			font-size: 28rpx;
			color: #333;
		}
		
		.amount {
			font-size: 36rpx;
			color: #ff5151;
			font-weight: bold;
		}
	}
	
	.submit-btn {
		width: 240rpx;
		height: 80rpx;
		background-color: #ff5151;
		color: #fff;
		border-radius: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
	}
}
</style> 